var t = getApp(), e = t.requirejs('core');
Page({
    data: {
        id: "",
        data: {},
        total: 1,   // 需要购买的数量
        totalNum: '',  // 支付总数
    },
    onLoad(s) {
        this.setData(s);
        e.get("syt.merch.tao_detail", s, res => {
            console.log(res);
            this.setData({
                data: res,
                id: s.id,
                totalNum: res.list.cardmoney
            })
        })
    },
    // 增加数量
    add() {
        this.setData({
            total: this.data.total + 1
        }), this.calculate();
    },
    // 减少数量
    sub() {
        if(this.data.total == 1) {
            wx.showToast({
                title: "最少为一个",
                icon: 'none'
            })
        } else {
            this.setData({
                total: this.data.total - 1
            }), this.calculate();
        }
    },
    calculate() {   // 计算支付金额
        let num = this.data.total, price = parseFloat(this.data.data.list.cardmoney);
        let result = (price * 100 * num / 100).toFixed(2);
        this.setData({
            totalNum: result
        })
    },
    submit(s) {
        let data = {
            id: this.data.id,
            total: this.data.total,
            price: this.data.totalNum
        };
        Object.assign(data, s.detail.value);
        e.get("syt.merch.tao_submit", data, res => {
            wx.showToast({
                title: res.message,
                icon: 'none'
            })
            if(res.success == 1) {
                wx.navigateTo({
                    url: '/project/new/pay?id=' + res.id,
                })
            }
        })
    }
})